package de.tsystems.jsfdict.beans;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.util.Map;
import java.util.TreeMap;

@Named("themeSwitcherBean")
@Scope("application")
@Controller
public class ThemeSwitcherBean {

    private Map<String, String> themes;
    private String theme = getDefaultTheme();


    public Map<String, String> getThemes() {
        return themes;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    @PostConstruct
    public void init() {
        theme = getDefaultTheme();

        themes = new TreeMap<String, String>();
        themes.put("Aristo", "aristo");
        themes.put("Black-Tie", "black-tie");
        themes.put("Hot-Sneaks", "hot-sneaks");
        themes.put("Le-Frog", "le-frog");
        themes.put("South-Street", "south-street");
        themes.put("Vader", "vader");

        // if u like u can add additioanal theme in pom and then add ui themes
        // see http://www.primefaces.org/showcase-labs/ui/themeswitcher.jsf
    }

    public void saveTheme() {
        setThemeInContext(theme);
    }


    private String getDefaultTheme() {
        String theme = "aristo";
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        if (params.containsKey("theme")) {
            theme = params.get("theme");
        }

        return theme;
    }

    private void setThemeInContext(String theme) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        if (externalContext.getRequestParameterMap().containsKey("theme")) {
            FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().put("theme", theme);
        }
    }
}
